package com.starsoft.smdc.crawler.joinquant;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.starsoft.frame.fileimport.CsvReader;
import com.starsoft.frame.fileimport.pojo.Column;
import com.starsoft.frame.fileimport.pojo.DataTable;
import com.starsoft.frame.fileimport.pojo.NullType;
import com.starsoft.frame.fileimport.pojo.Table;
import com.starsoft.frame.fileimport.pojo.Table.TitleType;
import com.starsoft.frame.fileimport.pojo.ValueType;
import com.starsoft.frame.util.DateUtil;
import com.starsoft.frame.util.MapUtil;
import com.starsoft.smdc.bean.SmdcMarketDaily;
import com.starsoft.smdc.bean.SmdcMarketDailyId;
import com.starsoft.smdc.util.SmdcConst;

@Service
public class SmdcJoinQuantUpdate extends JoinQuantService{

	private static Logger logger = LoggerFactory.getLogger(SmdcJoinQuantUpdate.class);

	private static final String INDEX_VALUATION_PATH = "/mydata/indexvaluation";
	private static final String LAST_DOWN_DATE = "JOIN_QUANT_LASTUPDATE";
	
	public String updateAll() {		
		Date beginDate = getBeginDate();
		List<SmdcMarketDaily> market = updateIndexValuation(beginDate);
		updateBeginDate();
		return "更新指数估值"+market.size();
	}

	protected List<SmdcMarketDaily> updateIndexValuation(Date begin){
		List<SmdcMarketDaily> result = new ArrayList<SmdcMarketDaily>();
		List<String> pathList= getFilePath(INDEX_VALUATION_PATH, begin);
		String downLoadApi = "downLoad";
		for (String path : pathList) {
			InputStream fileIs = downLoad(downLoadApi, path);
			List<SmdcMarketDaily> marketList = parseIndexValuation(fileIs);
			try {
				fileIs.close();
			} catch (IOException e) {
			}
			marketDailyDao.saveOrUpdateBatch(marketList);
			result.addAll(marketList);
		}
		logger.info("Update Index Valuation "+ result.size());
		return result;
	}
	
	protected List<SmdcMarketDaily> parseIndexValuation(InputStream is){
		CsvReader reader = new CsvReader();
		List<Column> columnList = new ArrayList<Column>();
		columnList.add(new Column("code", "secId", ValueType.String, NullType.notnull));
		columnList.add(new Column("date", "tradeDate", ValueType.Date, NullType.notnull, "yyyy-MM-dd"));
		columnList.add(new Column("pe", "pe", ValueType.Double, NullType.notnull));
		columnList.add(new Column("peWeighted", "peWeighted", ValueType.Double, NullType.notnull));
		columnList.add(new Column("pb", "pb", ValueType.Double, NullType.notnull));
		columnList.add(new Column("pbWeighted", "pbWeighted", ValueType.Double, NullType.notnull));
		columnList.add(new Column("roe", "roe", ValueType.Double, NullType.notnull));
		columnList.add(new Column("roeWeighted", "roeWeighted", ValueType.Double, NullType.notnull));
		columnList.add(new Column("div", "divi", ValueType.Double, NullType.notnull));
		columnList.add(new Column("divWeighted", "diviWeighted", ValueType.Double, NullType.notnull));
		Table table = new Table(TitleType.OnlyCode, columnList);
		DataTable  dataTable=reader.read(is, table);

		List<SmdcMarketDaily> marketDailyList = new ArrayList<SmdcMarketDaily>();
		for (Map<String, Object> mapObject : dataTable.getData()) {
			String secId = convertSecId((String) mapObject.get("secId"), true);
			mapObject.put("secId", secId);
			SmdcMarketDailyId id = new SmdcMarketDailyId(secId, (Date) mapObject.get("tradeDate"));
			SmdcMarketDaily marketDaily = marketDailyDao.get(id);
			if(marketDaily==null){
				continue;
			}
			MapUtil.updateBean(marketDaily, mapObject);
			marketDailyList.add(marketDaily);
		}
		return marketDailyList;
	}
	
	private Date getBeginDate(){
		String beginDateStr = preferenceService.getPreference(LAST_DOWN_DATE);
		Date beginDate = null;
		try {
			beginDate = DateUtil.toDate(beginDateStr, "yyyy-MM-dd HH:mm:ss");
		} catch (Exception e) {
			beginDate = SmdcConst.SMDC_BEGIN_DATE;
		}
		return beginDate;
	}
	
	private Date updateBeginDate(){
		Date now = new Date();
		String beginDateStr = DateUtil.toString(now, "yyyy-MM-dd HH:mm:ss");
		preferenceService.saveValue(LAST_DOWN_DATE, beginDateStr, null);
		return now;
	}
	
	
}
